//
//  CategoryManagerViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 29.03.25.
//

import Foundation
class CategoryManagerViewModel : KoiAccountingBaseViewModel {
    
    // 支出类别集合
    @Published var expenseList = Array<AccountingCategory>()
    // 收入类别集合
    @Published var incomeList = Array<AccountingCategory>()
    
    @Published var subCategoryListData:[String: Array<AccountingCategory>] = [String: Array<AccountingCategory>]()
    
    @Published var deleteState = ""
    
    @Published var changeState = ""
    
    @Published var transferState = ""
    
    private let categoryDao = KoiAccountingDB.standard.getCategoryDao()
    
    private let detailDao = KoiAccountingDB.standard.getDetailDao()
    
    
    func loadCategorys() {
        logger.debug("loadCategorys")
        DispatchQueue.global(qos: .background).async {
            let expenseResult = self.categoryDao.getCategoryByType(uid: self.uid, bookId: self.bookId, type: CategoryType.expense.rawValue)
            let incomeResult = self.categoryDao.getCategoryByType(uid: self.uid, bookId: self.bookId, type: CategoryType.income.rawValue)
            DispatchQueue.main.async {
                self.expenseList = expenseResult
                self.incomeList = incomeResult
            }
        }
    }
    
    func loadSubCategorys(parent: AccountingCategory) {
        let parentId = parent.cid
        if (parentId == nil) {
            return
        }
        DispatchQueue.global(qos: .background).async {
            let result = self.categoryDao.getSubCategoryListByParentId(uid: self.uid, bookId: self.bookId, parentId: parentId!)
            DispatchQueue.main.async {
                self.subCategoryListData[parentId!] = result
            }
        }
    }
    
    func transferCategory(oldCid: String? , newCid: String?) {
        guard let oldCid = oldCid else { return }
        guard let newCid = newCid else { return }
        asyncBackground {
            self.detailDao.transferCategory(uid: self.uid, bookId: self.bookId, oldCid: oldCid, newCid: newCid)
            self.asyncMain {
                self.transferState = "success"
            }
        }
    }
    
    func deleteCategory(selectedCategory: AccountingCategory?) {
        guard let category = selectedCategory else { return }
        asyncBackground {
            self.categoryDao.delete(category:category)
            self.asyncMain {
                self.deleteState = "success"
            }
        }
    }
    
    func changeToFirstCategory(selectedCategory: AccountingCategory?) {
        guard var category = selectedCategory else { return }
        category.changeToFirst()
        asyncBackground {
            self.categoryDao.update(category:category)
            self.asyncMain {
                self.changeState = "success"
            }
        }
    }
    
    func changeToSubCategory(selectedCategory: AccountingCategory?, parentCategory: AccountingCategory?) {
        guard var category = selectedCategory else { return }
        guard let parentId = parentCategory?.cid else { return }
        category.changeToSub(parentId: parentId)
        asyncBackground {
            self.categoryDao.update(category:category)
            self.asyncMain {
                self.changeState = "success"
            }
        }
    }
}
